package com.calvin.study.multithread.threadpool;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

import com.calvin.study.utils.ThreadUtil;

public class ThreadPoolDemo04 {

	static public class SimpleThreadFactory implements ThreadFactory {

		static AtomicInteger threadNo = new AtomicInteger(1);

		@Override
		public Thread newThread(Runnable target) {
			String threadName = "simpleThread-" + threadNo.get();
			System.out.println("创建一条线程， 名称为： " + threadName);
			threadNo.incrementAndGet();
			// 设置线程名称和异步执行目标
			Thread thread = new Thread(target, threadName);
			// 设置为守护线程
			// thread.setDaemon(true);
			return thread;
		}
	}

	// 自定义拒绝策略
	public static class CustomIgnorePolicy implements RejectedExecutionHandler {
		public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
			// 可做日志记录等
			System.out.println(r + " rejected; " + " - getTaskCount: " + e.getTaskCount());
		}
	}

	static class TargetTask implements Runnable {
		static AtomicInteger taskNo = new AtomicInteger(1);

		private String taskName;

		public TargetTask() {
			taskName = "task-" + taskNo.get();
			taskNo.incrementAndGet();
		}

		public void run() {
			System.out.println("任务： " + taskName + " doing");
			// 线程睡眠一会儿
			try {
				TimeUnit.SECONDS.sleep(500);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.println(taskName + " 运行结束.");
		}
	}

	public static void main(String[] args) throws InterruptedException {
		int corePoolSize = 2; // 核心线程数
		int maximumPoolSize = 4; // 最大线程数
		long keepAliveTime = 10;
		TimeUnit unit = TimeUnit.SECONDS;
		// 最大排队任务数
		BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<>(2);
		// 线程工厂
		ThreadFactory threadFactory = new SimpleThreadFactory();
		// 拒绝和异常处理策略
		RejectedExecutionHandler policy = new CustomIgnorePolicy();
		ThreadPoolExecutor pool = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
				threadFactory, policy);
		// 预启动所有核心线程
		pool.prestartAllCoreThreads();
		for (int i = 1; i <= 10; i++) {
			pool.execute(new TargetTask());
		}
		// 等待10秒
		TimeUnit.SECONDS.sleep(10);
		System.out.println("关闭线程池");
		pool.shutdown();
	}

}

class ThreadPoolDemo {

	public static void main(String[] args) {
		ThreadPoolExecutor executor = new ThreadPoolExecutor(10, 20, 100, TimeUnit.SECONDS,
				new LinkedBlockingDeque<Runnable>(30),new CallerRunsPolicy());

		for (int i = 0, len = 50; i < len; i++) {
			final int taskIndex = i;
			executor.execute(() -> {
				System.out.println("Thread-name:"+ThreadUtil.getCurThreadName());
				// 极端测试： 无限制睡眠
				try {
					Thread.sleep(5000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			});
			
		}
		

		
		executor.shutdown();
//		while (true) {
//			System.out.println("- activeCount:" + executor.getActiveCount() + " - taskCount:" + executor.getTaskCount());
//			ThreadUtils.sleepSeconds(1);
//		}
	}
}
